package com.cskaoyan.th58.config;

import com.cskaoyan.th58.mapper.UserMapper;
import com.cskaoyan.th58.mapper.UserMapperImpl;
import com.cskaoyan.th58.mapper.UserMapperImpl2;
import com.cskaoyan.th58.service.OrderService;
import com.cskaoyan.th58.service.OrderServiceImpl;
import com.cskaoyan.th58.service.UserService;
import com.cskaoyan.th58.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Macro_Ray
 * @since 2024/5/12 下午11:50
 */
// 声明这是一个配置类
@Configuration
public class SpringConfig {

    // 希望向 Spring容器中注册哪个组件，那么就写那个对象的创建语句
    @Bean
    public UserService userService(UserMapper userMapper){
        UserServiceImpl userService = new UserServiceImpl();
        userService.setUserMapper(userMapper);
        return userService;
    }

    // Spring 处理过程：
    // 1.根据方法的返回值类型，得知最终注入到Spring容器中的是orderService类型的对象
    // 2.方法的名称叫做orderService，所以注册到Spring容器中的对象编号为orderService
    // 3.方法的形参列表有一个叫做UserMapper，所以 Spring会扫描容器，从容其中取出一个叫UserMapper的实例对象，在调用当前方法时传递进来
    @Bean
    // 如果某个类型的实例对象有多个的话，并且希望指定从容器中获取对应的实例对象，可以使用 Qualifier注解
    public OrderService orderService(@Qualifier("userMapper2") UserMapper userMapper){
        OrderServiceImpl orderService = new OrderServiceImpl();
        orderService.setUserMapper(userMapper);
        return orderService;
    }

    @Bean
    public UserMapper userMapper(){
        UserMapper userMapper = new UserMapperImpl();
        return userMapper;
    }

    @Bean
    public UserMapper userMapper2(){
        UserMapper userMapper = new UserMapperImpl2();
        return userMapper;
    }
}
